$(function(){
	function Page(){
        this.init();
    }
	
	var isMember = true;  //是否印象城会员
	var freeTimes = 1;     //免费次数
	
	var isWating=false,wait=60,cd=null;
	
	var type;
	
	var lotteryType;
	
	var resultData;
	
	var canClick = true;
	
    Page.prototype = {
    	curPage: 1,
    	rightNum: 0,
    	startTime: 0,
    	endTime: 0,
    	canAnswer: true,
    	init: function(){
    		this.render();
    		this.bind();
    	},
    	bind: function(){
    		this.bindRule();
    		this.bindGl();
    		this.bindTip();
    		this.bindHome();
    		this.bindStart();
    		this.bindAnswer();
    		this.bindReplay();
    		this.bindShare();
    		this.bindLotteryLayer();
    		this.bindFree();      //免费抽奖
    		this.bindJifen();     //积分抽奖
    		this.bindRegister();  //注册印象城会员
    		this.getAuthCode();   //获取验证码
    		this.bindPerfect();   //完善资料
    	},
    	render: function(){
    		
    	},
    	bindRule: function(){
    		var that = this;
    		$(".ui-btn-rule").click(function(){
				$(".page-1").hide();
				$(".page-2").show();
				that.curPage = 2;
			})
    	},
    	bindGl: function(){
    		var that = this;
    		$(".ui-btn-gl").click(function(){
    			$(".page-1").hide();
				$(".page-3").show();
				that.curPage = 3;
    		})
    	},
    	bindTip: function(){
    		$(".local").click(function(){
    			$(".local img").hide();
    			$(this).find("img").show();
    		})
    	},
    	bindHome: function(){
    		var that = this;
    		$(".ui-home").click(function(){
    			$(".page-3").hide();
				$(".page-1").show();
				that.curPage = 1;
    		})
    	},
    	bindStart: function(){
    		var that = this;
    		$(".ui-start").click(function(){
	    		$(".page-"+that.curPage).hide();
	    		$(".page-4").show();
	    		that.curPage = 4;
	    		that.startTime = new Date();
    		})
    	},
    	bindAnswer: function(){
    		var that = this;
    		$(".answer-box li").click(function(){
    			if(that.canAnswer){
    				that.canAnswer = false;
    				if($(this).data("type")){
	    				$(this).addClass("state-1");
	    				that.rightNum++;
	    			}else{
	    				$(this).addClass("state-2");
	    			}
	    			var curQuestion = $(this).parents(".question");
	    			if(curQuestion.next().length){
	    				setTimeout(function(){
		    				curQuestion.addClass("hide");
		    				curQuestion.next().addClass("show").css({zIndex:3});
		    				that.canAnswer = true
		    			},500);
	    			}else{
	    				//答完题
	    				$(".num").text(that.rightNum)
	    				that.endTime = Math.round((new Date() - that.startTime)/1000);
	    				setTimeout(function(){
		    				$(".page-4").hide();
		    				$(".page-5").show();
		    				that.curPage = 5;
		    			},500);
		    			that.setResult();   //发送答题结果(答对题数量)
	    			}
    			}
    		})
    	},
    	bindLotteryLayer: function(){
    		var that = this;
    		$(".ui-lottery").click(function(){
    			var num = that.rightNum;
    			if(num<5){
    				$(".layer-nomatch").show();
	    			setTimeout(function(){
	    				$(".layer-nomatch").addClass("show");
	    			},30)
    			}else{
    				$(".layer-lottery").show();
	    			setTimeout(function(){
	    				$(".layer-lottery").addClass("show");
	    			},30)
    			}
    		})
    	},
    	bindReplay: function(){
    		var that = this;
    		$(".replay").click(function(){
    			$(".layer").hide().removeClass("show");
    			$(".answer-box li").removeClass();
    			that.renderQuestion();
    			$(".page-"+that.curPage).hide();
    			$(".page-4").show();
    			that.curPage = 4;
    		});
    	},
    	renderQuestion: function(){
    		this.rightNum = 0;
    		this.canAnswer = true;
    		$(".question").removeClass("hide show").removeAttr("style");
    	},
    	bindShare: function(){
    		$(".ui-invit").click(function(){
    			$(".share-box").show();
				setTimeout(function(){
					$(".share-box").addClass("show");
				},100);
    		});
    		
    		$(".share-box").click(function(){
				$(this).removeClass("show");
				setTimeout(function(){
					$(".share-box").hide();
				},300);
			});
    	},
    	setResult: function(){
    		var num = this.rightNum;
    		$.ajax({
    			type:"get",
    			url:"data/setResult.json",
    			dataType: "json",
    			data: {num:num},
    			success: function(data){
    				
    			}
    		});
    	},
    	bindFree: function(){
    		var that = this;
    		$(".free").click(function(){
    			if(!canClick){
    				return;
    			}
    			canClick = false;
    			lotteryType = "free";
    			var aniDom = $(".ui-box")[0];
    			if(freeTimes){
    				var num = that.rightNum;
    				$(".ui-box").addClass("tada");
	    			$.ajax({
		    			type:"get",
		    			url:"data/lottery.json",
		    			dataType: "json",
		    			data: {num:num},
		    			success: function(data){
		    				resultData = data;
		    				aniDom.addEventListener("webkitAnimationEnd", that.renderResult,  false);
		    			}
		    		});
    			}else{
    				layer.open({
						content: "免费次数已用完<br/>请使用积分抽奖",
						shadeClose: false,
						time: 3,
						className: "layer-alert"
					})
    				canClick = true;
    			}
    		})
    	},
    	bindJifen: function(){
    		var that = this;
    		var num = this.rightNum;
    		$(".jifen").click(function(){
    			if(!canClick){
    				return;
    			}
    			canClick = false;
    			var num = that.rightNum;
    			lotteryType = "jifen";
    			if(!isMember){
    				$(".layer-lottery").hide().removeClass("show");
    				$(".layer-register2").show();
    				canClick = true;
	    			setTimeout(function(){
	    				$(".layer-register2").addClass("show");
	    			},30)
	    			return;
    			}
    			$(".ui-box").addClass("tada");
    			var aniDom = $(".ui-box")[0];
    			$.ajax({
	    			type:"get",
	    			url:"data/lottery.json",
	    			dataType: "json",
	    			data: {num:num},
	    			success: function(data){
	    				resultData = data;
	    				aniDom.addEventListener("webkitAnimationEnd", that.renderResult, false);
	    			}
	    		});
    		});
    	},
    	renderResult: function(e){
    		var that = this;
    		var aniDom = $(".ui-box")[0];
    		var data = resultData;
    		aniDom.className = aniDom.className.replace('tada', '');
    		canClick = true;
    		$(".layer-lottery").hide().removeClass("show");
    		if(lotteryType=="free"){
    			freeTimes--;
    		}
    		aniDom.removeEventListener("webkitAnimationEnd", that.renderResult, false);
			if(data.success==1){
				if(data.status){
					$(".prizeName").text(data.prizeName);
					if(isMember){
						$(".layer-success").show();
		    			setTimeout(function(){
		    				$(".layer-success").addClass("show");
		    			},30)
					}else{
						$(".layer-register").show();
		    			setTimeout(function(){
		    				$(".layer-register").addClass("show");
		    			},30)
					}
				}else{
					$(".layer-fail").show();
	    			setTimeout(function(){
	    				$(".layer-fail").addClass("show");
	    			},30)
				}
			}else{
				layer.open({
					content: data.msg,
					shadeClose: false,
					time: 3,
					className: "layer-alert"
				})
			}
    	},
    	bindRegister: function(){
    		var that = this;
    		$(".ui-bind").click(function(){
    			type = $(this).data("type");
    			var phone = $.trim($(this).parents(".layer").find("#phone").val());
				var authcode = $.trim($(this).parents(".layer").find("#authcode").val());
				if(phone==""){
					layer.open({
						content: "请输入手机号",
						shadeClose: false,
						time: 3,
						className: "layer-alert"
					})
					return;
				}
				if (!/^1[34578]\d{9}/.test(phone)) {
					layer.open({
						content: "请输入正确的手机号码",
						shadeClose: false,
						time: 3,
						className: "layer-alert"
					})
					return;
				}
				if(authcode==""){
					layer.open({
						content: "请输入验证码",
						shadeClose: false,
						time: 3,
						className: "layer-alert"
					})
					return;
				}
				$.ajax({
					type:"get",
					url:"data/validate.json",
					dataType: 'json',
					data: {phone:phone,authcode:authcode},
					success: function(data){
						if(data.success){
							clearInterval(cd);
							isWating = false;
							$(".countDown").hide();
							$(".ui-get").show();
							wait = 60;
							$(".layer-register2").hide().removeClass("show");
							if(data.filledname==1){
								that.renderInfo();
								return;
							}
							if(type=="pure"){
								$(".layer-lottery").show();
				    			setTimeout(function(){
				    				$(".layer-lottery").addClass("show");
				    			},30);
							}else{
								$(".layer-tip").show();
				    			setTimeout(function(){
				    				$(".layer-tip").addClass("show");
				    			},30);
							}
			    			isMember = true;
						}else{
							layer.open({
								content: data.msg,
								shadeClose: false,
								time: 3,
								className: "layer-alert"
							})
						}
					}
				});
    		})
    	},
    	getAuthCode: function(){
    		$(".getCode").click(function(){
    			var phone = $.trim($(this).parents(".layer").find("#phone").val());
				if(phone==""){
					layer.open({
						content: "请输入手机号",
						shadeClose: false,
						time: 3,
						className: "layer-alert"
					})
					return;
				}
				if (!/^1[34578]\d{9}/.test(phone)) {
					layer.open({
						content: "请输入正确的手机号码",
						shadeClose: false,
						time: 3,
						className: "layer-alert"
					})
					return;
				}
				
				if(!isWating){
					$.ajax({
						type:"get",
						url:"data/validate.json",
						dataType: 'json',
						data: {},
						success: function(data){
							if(data.success){
								layer.open({
									content: "验证码已发送到手机",
									shadeClose: false,
									time: 3,
									className: "layer-alert"
								})
								isWating = true;
								$(".countDown").css({"display":"block"});
								$(".ui-get").hide();
								cd = setInterval(function(){
									if(wait==0){
										clearInterval(cd);
										isWating = false;
										$(".countDown").hide();
										$(".ui-get").show();
										wait = 60;
										return false;
									}
									$(".countDown").text(wait);
									wait--;
								},1000);
							}else{
								layer.open({
									content: data.msg,
									shadeClose: false,
									time: 3,
									className: "layer-alert"
								})
							}
						}
					});
				}else{
					layer.open({
						content: "你已经获取过了，<br/>请稍后再重新获取",
						shadeClose: false,
						time: 3,
						className: "layer-alert"
					})
				}
    		})
    	},
    	renderInfo: function(){
    		$(".layer-info").show();
			setTimeout(function(){
				$(".layer-info").addClass("show");
			},30);
    	},
    	bindPerfect: function(){
    		$(".ui-complete").click(function(){
    			var username = $("#username").val();
    			var birth = $("#birth").val();
    			if(username==""){
    				layer.open({
						content: "请填写您的姓名",
						shadeClose: false,
						time: 3,
						className: "layer-alert"
					})
    				return;
    			}
    			if(birth==""){
    				layer.open({
						content: "请填写您的生日",
						shadeClose: false,
						time: 3,
						className: "layer-alert"
					})
    				return;
    			}
    			var value = $("#infoForm").serialize(); 
    			$.ajax({
					type:"get",
					url:"data/info.json",
					dataType: 'json',
					data: value,
					success: function(data){
						if(data.success){
							$(".layer-info").hide().removeClass("show");
							if(type=="pure"){
								$(".layer-lottery").show();
				    			setTimeout(function(){
				    				$(".layer-lottery").addClass("show");
				    			},30);
							}else{
								$(".layer-tip").show();
				    			setTimeout(function(){
				    				$(".layer-tip").addClass("show");
				    			},30);
							}
			    			isMember = true;
						}else{
							layer.open({
								content: data.msg,
								shadeClose: false,
								time: 3,
								className: "layer-alert"
							})
						}
					}
				});
    		})
    	}
    }
    new Page();
    
    $(".tip").addClass("show");
})
